﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using ProjectWizardManagementv1.Infrastructure.Concrete;

namespace ProjectWizardManagementv1.WpfComponents.UserControls
{
    /// <summary>
    /// Interaction logic for AdvanceRichTextEditor.xaml
    /// </summary>
    public partial class AdvanceRichTextEditor : UserControl
    {
        public string DestinationPath { get; set; }

        private OpenFileDialog _pictureDialog;
        public AdvanceRichTextEditor()
        {
            InitializeComponent();
            _pictureDialog = new OpenFileDialog
            {
                Filter = "All Pictures|*.png; *.jpeg; *.bmp; *.gif;",
                Title = "Add Image",
                Multiselect = false
            };
        }

        private void FontSize_ApplySelectedSize(object sender, EventArgs e)
        {
            var size = (string) FontSize.SelectedItem;
            ApplyTextProperty(FontSizeProperty, size);
        }

        private void FontType_ApplySelectedFont(object sender, EventArgs e)
        {
            var fontName = (string)FontType.SelectedItem;
            ApplyTextProperty(FontFamilyProperty, fontName);
        }

        private void ColorPicker_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
        {
            var fontColor = ColorPicker.SelectedColor;
            ApplyTextProperty(ForegroundProperty, new SolidColorBrush(fontColor));
        }

        private void ApplyTextProperty(DependencyProperty property, object value)
        {
            var textBox = GetFocusedTextBox();
            if(textBox != null && value != null)
            {
                textBox.Selection.ApplyPropertyValue(property, value);
                textBox.Focus();
            } 
        }

        private RichTextBox GetFocusedTextBox()
        {
            if(Body.IsFocused)
                return Body;
            if(Header.IsFocused)
                return Header;
            if(Footer.IsFocused)
                return Footer;

            return null;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {


        }

        private void Picture_AddPicture(object sender, RoutedEventArgs e)
        {
            var textbox = GetFocusedTextBox();

            if(_pictureDialog.ShowDialog().HasValue)
            {
                var image = new Image
                                {
                                    Source = new BitmapImage(new Uri(_pictureDialog.FileName, UriKind.Relative)) ,
                                    Width = 80,
                                    Height = 80
                                };
                var startPostion = textbox.Selection.Start;
                var run = new Run("", startPostion);
                run.SiblingInlines.Add(image);
            }

        }

        private void Save_SaveDocument(object sender, RoutedEventArgs e)
        {
            WordDocxDocumentIO.SaveDocument(new TextRange(Body.Document.ContentStart, Body.Document.ContentEnd), DestinationPath);
        }

    }

    public class FontList : ObservableCollection<string>
    {
        public FontList()
        {
            var fonts = new List<string>();
            Fonts.SystemFontFamilies.ToList().ForEach(f => fonts.Add(f.ToString()));
            fonts.OrderBy(f=>f).ToList().ForEach(this.Add);
        }
    }

    public class FontSize : ObservableCollection<string>
    {
        public FontSize()
        {
            this.AddFontSizes(new List<string>{"8","9","10","11","12","14","16","18", "20","22","24","26",
            "28","36","48","72"});  
        }

        private void AddFontSizes(List<string> sizes)
        {
            sizes.ForEach(this.Add);
        }
    }
}
